package graduate.cslg.com.universityshopping.card;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import graduate.cslg.com.universityshopping.R;
import graduate.cslg.com.universityshopping.net.LoginResponse;
import graduate.cslg.com.universityshopping.net.RetrofitDevelop;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class PickRegistration extends AppCompatActivity {

    private EditText yourName;
    private EditText yourId;
    private EditText yourTel;
    private Button submitButton;
    private LoginResponse mLoginResponse;
    String getName, getAccount, getPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pick_registration);
        initViews();
        submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getName = yourName.getText().toString();
                getAccount = yourId.getText().toString();
                getPhone = yourTel.getText().toString();
                if (getName.length() != 0 && getAccount.length() != 0 && getPhone.length() != 0) {
                    sendMessage(getName, getAccount, getPhone);
                } else {
                    Toast.makeText(PickRegistration.this, "请输入完整信息！", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initViews() {
        yourName = findViewById(R.id.your_name);
        yourId = findViewById(R.id.your_id);
        yourTel = findViewById(R.id.your_phone);
        submitButton = findViewById(R.id.submit);
    }

    /**
     * 发送捡卡信息
     * @param name 卡主名字
     * @param account 卡主学号
     * @param phone 捡卡人手机号
     */
    private void sendMessage(String name, String account, String phone) {
        RetrofitDevelop.getAccountService()
                .pickRegister(name, account, phone)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LoginResponse>() {
                    @Override
                    public void onCompleted() {
                        if (mLoginResponse.getStatus().equals("true")) {
                            Toast.makeText(PickRegistration.this, "信息提交成功！", Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            Toast.makeText(PickRegistration.this, "信息提交失败，请检查输入信息是否正确", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        Toast.makeText(PickRegistration.this, "网络错误，请稍后再试！", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(LoginResponse loginResponse) {
                        mLoginResponse = loginResponse;
                    }
                });
    }
}
